layui.define(['table', 'form', 'upload', 'admin'], function (exports) {
    var $ = layui.$
        , table = layui.table
        , form = layui.form
        , upload = layui.upload
        , admin = layui.admin;


    table.on("sort(LAY-user-manage)", function (obj) {
        table.reload("LAY-user-manage", {
            initSort: obj,
            where: {
                field: obj.field, //排序字段   在接口作为参数字段  field order
                order: obj.type, //排序方式   在接口作为参数字段  field order
            },
        });
    });

    // 专家列表
    table.render({
        elem: '#LAY-complabel-manage'
        , url: '/admin/complabel/getlist'
        , cols: [[
            {field: 'id', width: 120, title: 'ID', sort: true}
            , {
                field: '', title: '联赛类型', width: 120, templet: function (d) {
                    switch (d.sport_id) {
                        case 1:
                            return '足球';
                        case 2:
                            return '篮球';
                    }
                    return '';
                }
            }
            , {field: 'label', title: '标签名称'}
            , {
                field: '', title: '联赛图片', width: 150, templet: function (d) {
                    if (d.label_pic) {
                        return '<img style="display: inline-block;height: 100%;" src= "' + d.label_pic + '" layadmin-event="iconPreview" value= "' + d.label_pic + '">';
                    }
                    return '';

                }
            }
            , {field: 'comp', title: '联赛名称', width: 250}

            , {field: 'serial', title: '排序', width: 250}

            , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-cmplabel-list'}
        ]]
        , page: true
        , limit: 20
        , height: 'full-140'
        , text: '对不起，加载出现异常！'
        , even: true
    });

    //监听工具条
    table.on('tool(LAY-complabel-manage)', function (obj) {
        if (obj.event === 'edit') {
            var index = layer.open({
                type: 2
                , title: '编辑联赛标签'
                , content: '/admin/complabel/edit?id=' + obj.data.id
                , maxmin: true
                , area: ['700px', '500px']
            });
        }

    });

    form.on('submit(complabel-add)', function (obj) {
        //提交
        admin.req({
            url: '/admin/complabel/add_post'
            , data: obj.field
            , success: function (res) {
                if (res.code == 0) {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                    parent.layer.msg(res.msg, {icon: 1});
                    parent.layui.table.reload('LAY-complabel-manage');
                }
            }
        });
        return false;
    });

    form.on('submit(complabel-edit)', function (obj) {
        //提交
        admin.req({
            url: '/admin/complabel/edit_post'
            , data: obj.field
            , success: function (res) {
                if (res.code == 0) {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                    parent.layer.msg(res.msg, {icon: 1});
                    parent.layui.table.reload('LAY-complabel-manage');
                }
            }
        });
        return false;
    });


    //上传头像
    var iconSrc = $('#LAY_iconSrc');
    var iconPreview = $('#LAY_iconPreview');
    upload.render({
        url: '/admin/index/noOperation'
        , elem: '#LAY_iconUpload'
        , auto: true
        , choose: function (obj) {
            layer.load();
            var choosefile;
            obj.preview(function (index, file, result) {
                choosefile = file;
            });
            admin.req({
                url: '/admin/index/signForCos'
                , success: function (res) {
                    if (res.code == 0) {
                        var cos = new COS({
                            getAuthorization: function (options, callback) {
                                var credentials = res.data.credentials;
                                callback({
                                    TmpSecretId: credentials.tmpSecretId,
                                    TmpSecretKey: credentials.tmpSecretKey,
                                    XCosSecurityToken: credentials.sessionToken,
                                    ExpiredTime: res.data.expiredTime
                                });
                            }
                        });
                        cos.putObject({
                            Bucket: res.data.bucket,
                            Region: res.data.region,
                            Key: 'images/' + res.data.filename + choosefile.name,
                            Body: choosefile,
                        }, function (err, data) {
                            layer.closeAll('loading');
                            if (err) {
                                console.log(err);
                                layer.msg("上传失败", {icon: 5});
                            } else {
                                if (data.statusCode == 200) {
                                    iconSrc.val("https://" + data.Location);
                                    iconPreview.attr("value", iconSrc.val());
                                } else {
                                    layer.msg("图片上传失败", {icon: 5});
                                }
                            }
                        });
                    } else {
                        layer.closeAll('loading');
                        layer.msg(res.msg, {icon: 5});
                    }
                }
            })
        }
    });

    //查看头像
    admin.events.iconPreview = function (othis) {
        var src = othis.attr("value");
        if (!src) {
            src = iconSrc.val();
        }
        layer.photos({
            photos: {
                "title": "查看" //相册标题
                , "data": [{
                    "src": src //原图地址
                }]
            }
            , shade: 0.01
            , closeBtn: 1
            , anim: 5
        });
    };

    exports('complabel', {})
});
